-- DamageAction
-- create by zouyb
-- 受创动作

DamageAction = class("DamageAction", function()
    return ActionBase.create();
end);

function DamageAction.create(source, target, damageHp, skillId, extra)
    local self = DamageAction.new();
    self.source = source;
    self.target = target;
    self.damageHp = damageHp;
    self.skillId = iif(skillId == nil, PHYSIC_ATTACK, skillId);
    self.extra = extra or {};
    return self;
end

-- 构造函数
function DamageAction:ctor()
end

-- 开始播放
function DamageAction:play()
    self.state = ActionState.PLAYING;

    local delay = 0;

    if self.target.type == OBJECT_TYPE_MONSTER then
        -- 玩家攻击怪物
        delay = self:monsterDamaged(self.source, self.target, self.damageHp, self.skillId, self.extra);
    elseif self.target.type == OBJECT_TYPE_USER then
        -- 怪物攻击玩家
        delay = self:heroDamaged(self.source, self.target, self.damageHp, self.skillId);
        -- 播放冈布奥受创音效
    elseif self.target.type == OBJECT_TYPE_BINE then
        -- 怪物攻击魔藤
        delay = self:bineDamaged(self.source, self.target, self.damageHp, self.skillId);
    elseif self.target.type == OBJECT_TYPE_SUMMON then
        -- 召唤兽受创
        delay = self:summonDamaged(self.source, self.target, self.damageHp, self.skillId);
    end

    self.state = ActionState.END;
    return delay;
end

-- 玩家攻击怪物
function DamageAction:monsterDamaged(source, target, damage, skillId, extra)
    local needDelay = false;

    -- 刷新起效次数
    if type(target.damageTimes) == "number" then
        if target.damageTimes <= 0 then
            target.delayDamageEffect = false;
        else
            target.damageTimes = target.damageTimes - 1;
        end
    end

    if target.delayDamageEffect then
        needDelay = target.delayDamageEffect;
    end

    EventMgr.fire(event.MONSTER_DAMAGED, {["damage"] = damage, ["pos"] = self.target:getOwner(),
        ["skillId"] = skillId, ["needDelay"] = needDelay, ["extra"] = extra, ["source"] = source});

    return 0;
end

-- 怪物攻击玩家
function DamageAction:heroDamaged(source, target, damage, skillId)
    -- 刷新起效次数
    if type(target.damageTimes) == "number" then
        if target.damageTimes <= 0 then
            target.delayDamageEffect = false;
        else
            target.damageTimes = target.damageTimes - 1;
        end
    end 

    if target.delayDamageEffect then
        needDelay = target.delayDamageEffect;
    end
    EventMgr.fire(event.HERO_DAMAGED, {["damage"] = damage, ["needDelay"] = needDelay});

    return 0;
end

-- 怪物攻击魔藤
function DamageAction:bineDamaged(source, target, damage, skillId)
    return 0;
end

-- 召唤物受创
function DamageAction:summonDamaged(source, target, damage, skillId)
    EventMgr.fire(event.SUMMON_DAMAGED, {["damage"] = damage, ["pos"] = self.target:getOwner(), });

    return 0;
end

return DamageAction;